<template>
  <div class="dashboard-editor-container">

    <!-- <github-corner style="position: absolute; top: 0px; border: 0; right: 0;"/> -->

    <panel-group
      :newCount="newCount"
      :unHirstryResCount="unHirstryResCount"
      :recheckCount="recheckCount"
      :todayUnsystemResCount="todayUnsystemResCount"
      :todayPingJiaCount="todayPingJiaCount"
      :todayUnsystemPingJia="todayUnsystemPingJia"
      @handleSetLineChartData="handleSetLineChartData">
    </panel-group>

    <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <line-chart :chart-data="lineChartData" :legend="legend"/>
    </el-row>

    <!-- <el-row :gutter="32">
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <raddar-chart/>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <pie-chart/>
        </div>
      </el-col>
      <el-col :xs="24" :sm="24" :lg="8">
        <div class="chart-wrapper">
          <bar-chart/>
        </div>
      </el-col>
    </el-row> -->

    <!-- <el-row :gutter="8">
      <el-col :xs="{span: 24}" :sm="{span: 24}" :md="{span: 24}" :lg="{span: 12}" :xl="{span: 12}" style="padding-right:8px;margin-bottom:30px;">
        <transaction-table/>
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 12}" :xl="{span: 12}" style="margin-bottom:30px;">
        <todo-list/>
      </el-col>
      <el-col :xs="{span: 24}" :sm="{span: 12}" :md="{span: 12}" :lg="{span: 12}" :xl="{span: 12}" style="margin-bottom:30px;">
        <box-card/>
      </el-col>
    </el-row> -->

  </div>
</template>

<script>
import GithubCorner from '@/components/GithubCorner'
import PanelGroup from './components/PanelGroup'
import LineChart from './components/LineChart'
import RaddarChart from './components/RaddarChart'
import PieChart from './components/PieChart'
import BarChart from './components/BarChart'
import TransactionTable from './components/TransactionTable'
import TodoList from './components/TodoList'
import BoxCard from './components/BoxCard'
import { init, user, resource, recheck } from '@/api/dashboard'

export default {
  name: 'DashboardAdmin',
  components: {
    GithubCorner,
    PanelGroup,
    LineChart,
    RaddarChart,
    PieChart,
    BarChart,
    TransactionTable,
    TodoList,
    BoxCard
  },
  data() {
    return {
      lineChartData: {},
      newCount: 0,
      recheckCount: 0,
      todayUnsystemResCount: 0,
      todayPingJiaCount: 0,
      todayUnsystemPingJia: 0,
      unHirstryResCount: 0,
      chartData: {
        newVisitis: {
          expectedData: [],
          actualData: []
        },
        unHirstryResCount: {
          expectedData: [],
          actualData: []
        },
        recheck: {
          expectedData: [],
          actualData: []
        },
        todayUnsystemRes: {
          expectedData: [],
          actualData: []
        },
        todayPingJia: {
          expectedData: [],
          actualData: []
        },
        todayUnsystemPingJia: {
          expectedData: [],
          actualData: []
        }
      },
      legend: ['未认证', '已认证']
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      init().then(({ status, data }) => {
        if (status) {
          this.newCount = data.user
          this.unHirstryResCount = data.resource
          this.recheckCount = data.recheck
        }
        this.getUser(true).then(data => {
          this.lineChartData = this.chartData.newVisitis
        })
      })
    },
    handleSetLineChartData(type) {
      switch (type) {
        case 'unHirstryResCount':
          this.legend = ['资源总数', '已审核总数']
          this.getResource()
          break
        case 'recheck':
          this.legend = ['已审核', '待审核']
          this.getRecheck()
          break
        case 'todayPingJia':
          this.legend = ['已评价', '未评价']
          break
        case 'newVisitis':
        default:
          this.getUser()
          this.legend = ['未认证', '已认证']
      }
      this.lineChartData = this.chartData[type]
    },
    getUser(init = false) {
      if (!init) {
        this.$store.commit('SHOW_LOADING')
      }
      return new Promise((resolve, reject) => {
        user().then(({ status, data }) => {
          this.$store.commit('HIDE_LOADING')
          if (status) {
            this.newCount = data.total
            this.chartData.newVisitis.expectedData = []
            this.chartData.newVisitis.actualData = []
            for (const item of data.list) {
              this.chartData.newVisitis.expectedData.push(item.expectedData)
              this.chartData.newVisitis.actualData.push(item.actualData)
            }
            if (!init) {
              this.lineChartData = this.chartData.newVisitis
            }
            resolve(data)
          } else {
            reject('error')
          }
        })
      })
    },
    getResource() {
      this.$store.commit('SHOW_LOADING')
      resource().then(({ status, data }) => {
        this.$store.commit('HIDE_LOADING')
        if (status) {
          this.unHirstryResCount = data.total
          this.chartData.unHirstryResCount.expectedData = []
          this.chartData.unHirstryResCount.actualData = []
          for (const item of data.list) {
            this.chartData.unHirstryResCount.expectedData.push(item.expectedData)
            this.chartData.unHirstryResCount.actualData.push(item.actualData)
          }
          this.lineChartData = this.chartData.unHirstryResCount
        }
      })
    },
    getRecheck() {
      this.$store.commit('SHOW_LOADING')
      recheck().then(({ status, data }) => {
        this.$store.commit('HIDE_LOADING')
        if (status) {
          this.recheckCount = data.total
          this.chartData.recheck.expectedData = []
          this.chartData.recheck.actualData = []
          for (const item of data.list) {
            this.chartData.recheck.expectedData.push(item.expectedData)
            this.chartData.recheck.actualData.push(item.actualData)
          }
          this.lineChartData = this.chartData.recheck
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  background-color: rgb(240, 242, 245);
  height: 100vh - 6vh;
  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}
</style>
